#include "GY_TreeComboBox.h"

#include <QDebug>
#include "MY_TreeComboBoxView.h"


GY_TreeComboBox::GY_TreeComboBox()
{
    //允许收缩
    m_canPopup = true;
    //不带复选
    m_isCheck = false;
    //未点击Item；
    m_isCheckItem = false;
    m_pTreeMode = new QStandardItemModel;
    m_pTreeView = new MY_TreeComboBoxView;
    this->setModel(m_pTreeMode);
    this->setView(m_pTreeView);
    //隐藏树的头
    m_pTreeView->header()->hide();
    //设置comboBox的linedit可以修改
    this->setEditable(true);
    connect(this,SIGNAL(activated(int)),SLOT(getCurrClickItem()));

    connect(static_cast<MY_TreeComboBoxView*>(m_pTreeView),&MY_TreeComboBoxView::treeMousePressed,this,[=](bool inItem){

        if(m_isCheck){
            m_canPopup = false;
            m_isCheckItem = !inItem;
        }else{
            m_canPopup = !inItem;
        }
    });
}
void GY_TreeComboBox::setColumnNumber(const int coluNumber)
{
    if(coluNumber <= 0){
        return;
    }
     m_pTreeMode->setColumnCount(coluNumber);
}

void GY_TreeComboBox::addTreeHead(QList<QString> &headList)
{
    if(headList.size() == 0){
        return;
    }
    //将树的头show出来
    m_pTreeView->header()->show();
    //设置树的列数
    m_pTreeMode->setColumnCount(headList.size());
    //设置树的头
    m_pTreeMode->setHorizontalHeaderLabels(headList);
}

void GY_TreeComboBox::getCurrNodeItem(QStandardItem *currItem, dataStruct *dataInfor)
{
    if(currItem == nullptr || !currItem->hasChildren()){
        return;
    }
    for (int i = 0; i < currItem->rowCount(); i++) {
        QStandardItem *childItem = currItem->child(i,0);
        //查找节点对应的信息
        QMap <QStandardItem *,QList <QVariant>>::iterator iter = m_itemToData.find(childItem);
        if(iter == m_itemToData.end()){
            continue;
        }
        dataStruct chilData;
        chilData.currRowData = iter.value();
        if(childItem->hasChildren()){
            //有子节点继续
            getCurrNodeItem(childItem,&chilData);
        }
        dataInfor->childData.push_back(chilData);
    }
}

void GY_TreeComboBox::checkCurrCodeAndChild(QStandardItem *currItem, bool isCheck)
{
    if(currItem == nullptr){
        return;
    }
    if(isCheck){
        currItem->setCheckState(Qt::Checked);
    }else {
       currItem->setCheckState(Qt::Unchecked);
    }
    if(!currItem->hasChildren()){
        return;
    }
    for (int child = 0;child < currItem->rowCount();child++) {
        QStandardItem *childItem = currItem->child(child,0);
        checkCurrCodeAndChild(childItem,isCheck);
    }
}

QString GY_TreeComboBox::getCheckName()
{
    QString allName;
    //查找节点对应的信息
    QMap <QStandardItem *,QList <QVariant>>::iterator iter;
    for(iter = m_itemToData.begin();iter != m_itemToData.end();iter++){
        if(iter.key()->checkState() != Qt::Unchecked){
            allName = allName + iter.key()->text() + ";";
        }
    }
    return allName;
}

void GY_TreeComboBox::changeParentCheck(QStandardItem *currItem)
{
    if(currItem == nullptr){
        return;
    }
    if(currItem->parent() == nullptr){
        return;
    }
    QStandardItem * parentItem = currItem->parent();
    int chileNUmber = parentItem->rowCount();
    int checkNumber = 0;
    for (int child = 0;child < parentItem->rowCount();child++) {
        QStandardItem *childItem = parentItem->child(child,0);
        if(childItem->checkState() == Qt::Checked){
            checkNumber++;
        }
    }
    if(chileNUmber == checkNumber){
        parentItem->setCheckState(Qt::Checked);
    }else if (checkNumber < chileNUmber && checkNumber != 0) {
        parentItem->setCheckState(Qt::PartiallyChecked);
    }else {
        parentItem->setCheckState(Qt::Unchecked);
    }
    changeParentCheck(parentItem);
}

QList<QVariant> GY_TreeComboBox::findItemTodata(QStandardItem *item)
{
    QList<QVariant> currData;
    //查找节点对应的信息
    QMap <QStandardItem *,QList <QVariant>>::iterator iter = m_itemToData.find(item);
    if(iter == m_itemToData.end()){
        return currData;
    }
    return iter.value();
}

dataStruct * GY_TreeComboBox::getCurrNodeAndChildInfor()
{
    dataStruct *infor = new dataStruct;
    if(m_pCurrItem == nullptr){
        return nullptr;
    }
    //查找节点对应的信息
    QMap <QStandardItem *,QList <QVariant>>::iterator iter = m_itemToData.find(m_pCurrItem);
    if(iter == m_itemToData.end()){
        return nullptr;
    }
    infor->currRowData = iter.value();
    if(!m_pCurrItem->hasChildren()){
        //没有子节点退出
        return infor;
    }
    getCurrNodeItem(m_pCurrItem,infor);
    return infor;
}

void GY_TreeComboBox::clearAllData()
{
    this->clear();
    m_pCurrItem = nullptr;
    m_data.clear();
    //清空节点映射
    m_itemToData.clear();
}

bool GY_TreeComboBox::changeTreeHead(QList<QString> &headList)
{
    if(headList.size() == 0){
        m_pTreeView->header()->hide();
        return true;
    }
    if(m_pTreeView->header()->isHidden()){
        return false;
    }
    m_pTreeMode->setColumnCount(headList.size());
    m_pTreeMode->setHorizontalHeaderLabels(headList);
    return true;
}

QList<QStandardItem *> GY_TreeComboBox::getCheckItem()
{
    QList<QStandardItem *> allCheckItem;
    if(!m_isCheck){
        return allCheckItem;
    }
    QMap <QStandardItem *,QList <QVariant>>::iterator iter;
    for(iter = m_itemToData.begin();iter != m_itemToData.end();iter++){
        if(iter.key()->checkState() == Qt::Checked){
            allCheckItem.append(iter.key());
        }
    }
    return allCheckItem;
}

void GY_TreeComboBox::addTreeData(QList<dataStruct> &data, bool isCheck)
{
    if(data.size() == 0){
        return;
    }
    m_isCheck= isCheck;
    m_data << data;
    //当前节点的行数
    int row = 0;
    QList<dataStruct>::iterator listIter;
    for (listIter = data.begin();listIter != data.end();listIter++) {
        int column = 0;
        QList <QVariant> currList = listIter->currRowData;
        QList <QVariant>::iterator dataIter;
        for (dataIter = listIter->currRowData.begin();
             dataIter != listIter->currRowData.end();
             dataIter++) {
            QStandardItem *item = new QStandardItem(dataIter->toString());
            m_pTreeMode->setItem(row,column,item);
            //添加子节点
            if(0 == column){
                if(isCheck){
                    item->setCheckable(true);
                }

                m_itemToData.insert(item,currList);
                if(listIter->childData.size() > 0){
                    addChild(item,listIter->childData);
                }
            }

            column++;
        }
        row++;
    }
    if(!isCheck){
        //初始化时，当前节点为第一个节点
        m_pCurrItem = m_pTreeMode->item(0,0);
    }


}
void GY_TreeComboBox::addChild(QStandardItem *item, QList<dataStruct> chaildList)
{
    //当前节点的行数
    int row = 0;

    QList<dataStruct>::iterator listIter;
    for (listIter = chaildList.begin();listIter != chaildList.end();listIter++) {
        int column = 0;
        QList <QVariant> currList = listIter->currRowData;
        QList <QVariant>::iterator dataIter;
        for (dataIter = listIter->currRowData.begin();
             dataIter != listIter->currRowData.end();
             dataIter++) {
            QStandardItem *itemChild = new QStandardItem(dataIter->toString());
           item->setChild(row,column,itemChild);
            //添加子节点
            if(0 == column){
                if(m_isCheck){
                    itemChild->setCheckable(true);
                }
                m_itemToData.insert(itemChild,currList);
                if(listIter->childData.size() > 0){
                    addChild(itemChild,listIter->childData);
                }

            }
            column++;
        }
        row++;
    }
}

void GY_TreeComboBox::getCurrCodePath(QStandardItem *curritem, QString &path)
{
    if(curritem->parent() == nullptr){
        //没有子节点退出
        path = curritem->text() + path;
        return;
    }
    path = "/" + curritem->text() + path;
    getCurrCodePath(curritem->parent(),path);
}

void GY_TreeComboBox::getCurrClickItem()
{
    if(m_isCheck){
        m_canPopup = true;
        if(!m_isCheckItem){
            this->setCurrentText(getCheckName());
            return;
        }
        //获取当前点击的item
        QModelIndex currIndex = m_pTreeView->currentIndex();
        QStandardItem *item = m_pTreeMode->itemFromIndex(currIndex);
        if(item == nullptr){
            return;
        }

        QStandardItem *parentItem = item->parent();
        if(parentItem == nullptr){

            m_pCurrItem = m_pTreeMode->item(item->row(),0);

        }else {
            m_pCurrItem = parentItem->child(item->row(),0);
        }
        if(m_pCurrItem->checkState() == Qt::Checked){
            checkCurrCodeAndChild(m_pCurrItem,false);
        }else{
            checkCurrCodeAndChild(m_pCurrItem,true);
        }
        changeParentCheck(m_pCurrItem);
        this->setCurrentText(getCheckName());

    }else {
        hidePopup();
        //获取当前点击的item
        QModelIndex currIndex = m_pTreeView->currentIndex();
        QStandardItem *item = m_pTreeMode->itemFromIndex(currIndex);
        if(item == nullptr){
            return;
        }

        QStandardItem *parentItem = item->parent();
        if(parentItem == nullptr){
            //无父节点直接显示当前名字
            m_pCurrItem = m_pTreeMode->item(item->row(),0);
            this->setCurrentText(m_pCurrItem->text());
            return;
        }
        m_pCurrItem = parentItem->child(item->row(),0);
        QString path;
        //获取当前节点路径
        getCurrCodePath(m_pCurrItem,path);
        this->setCurrentText(path);
    }

}

void GY_TreeComboBox::hidePopup()
{
    if(m_canPopup){
        QComboBox::hidePopup();
    }
}
